package com.example.wiki.service;

import com.example.wiki.domain.Ebook;
import com.example.wiki.domain.EbookExample;
import com.example.wiki.domain.User;
import com.example.wiki.domain.UserExample;
import com.example.wiki.exception.BusinessException;
import com.example.wiki.exception.BusinessExceptionCode;
import com.example.wiki.mapper.EbookMapper;
import com.example.wiki.mapper.UserMapper;
import com.example.wiki.req.*;
import com.example.wiki.resp.EbookResp;
import com.example.wiki.resp.PageResp;
import com.example.wiki.resp.UserLoginResp;
import com.example.wiki.resp.UserResp;
import com.example.wiki.util.CopyUtil;
import com.example.wiki.util.SnowFlake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;


@Service
public class UserService {
    private static final Logger Log = LoggerFactory.getLogger(UserService.class);
    @Autowired
    private SnowFlake snowFlake;
    @Autowired
    private UserMapper userMapper;

    public PageResp<UserResp> list(UserReq req){
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        if(!ObjectUtils.isEmpty(req.getLoginName())){
            criteria.andNameLike("%" + req.getLoginName() + "%");
        }

        // 分页

        PageHelper.startPage(req.getPage(), req.getSize());
        List<User> userList = userMapper.selectByExample(userExample);

        PageInfo<User> pageInfo = new PageInfo<>(userList);


        List<UserResp> userRespList = CopyUtil.copyList(userList, UserResp.class);
        PageResp<UserResp> PageResp = new PageResp<>();
        PageResp.setTotal(pageInfo.getTotal());
        PageResp.setList(userRespList);
        return PageResp;
    }
    public void save(UserSaveReq e){
//        try{
            User user = CopyUtil.copy(e,User.class);
            if(ObjectUtils.isEmpty(e.getId())){

                User userDB = selectByLoginName(user.getLoginName());
                if(!ObjectUtils.isEmpty(userDB)){
                    throw new BusinessException(BusinessExceptionCode.USER_LOGIN_NAME_EXIST);
                }else {
                    long l = snowFlake.nextId();
                    System.out.println(l);
                    user.setId(l);
                    userMapper.insert(user);
                }

            } else {

                userMapper.updateByExample(user);
            }

//        }catch (Exception exception){
//            exception.printStackTrace();
//
//        }

    }
    public int del(Long id){
        int i = userMapper.deleteByExample(id);
        return i;
    }
    /*
    * 用户名校验
    * */
    public User selectByLoginName(String LoginName){
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        UserExample.Criteria criteria1 = criteria.andLoginNameEqualTo(LoginName);
        List<User> userList = userMapper.selectByExample(userExample);
        if(ObjectUtils.isEmpty(userList)){
            return null;
        }else {
            return userList.get(0);
        }
    }
    /*
    * login
    * */
    public UserLoginResp login(UserLoginReq userLoginReq){
        User user = selectByLoginName(userLoginReq.getLoginName());
        if(ObjectUtils.isEmpty(user)){
            Log.info("用户名不存在，{}",userLoginReq.getLoginName());
            throw new BusinessException(BusinessExceptionCode.LOGIN_USER_ERROR);
        }else {
            if(user.getPassword().equals(userLoginReq.getPassword())){
                UserLoginResp userLoginResp = CopyUtil.copy(user,UserLoginResp.class);
                return  userLoginResp;
            }else {
                Log.info("密码错误，{}",userLoginReq.getLoginName());
                throw new BusinessException(BusinessExceptionCode.LOGIN_USER_ERROR);
            }
        }
    }
}
